//==========================================================  
// Dummy JSon to read data from. Purely for testing purpose
//==========================================================

//==========================================================  
// Initializes json data
//==========================================================
var initJsonData = function () {

    var data = GetJson();
    SplitDataArrayIntoMatrix(data);
    setupTileVariables(data);
}

//==========================================================  
// Setups global variables with Height, Width and Count for tiles
//==========================================================

function setupTileVariables(data) {
    tileHeight = data.tileheight;
    tileWidth = data.tilewidth;
    halfTileHeight = data.tileheight / 2;
    halfTileWidth = data.tilewidth / 2;
    tileXCount = data.height;
    tileYCount = data.width;
    map = data.layers[0].data;
    tileArray = splitTileSet(data.tilesets);
}

//==========================================================  
// Splits single array into [y][x] Matrix based on Height/Width
//==========================================================
function SplitDataArrayIntoMatrix(jsonData) {
    var tileMatrix = new Array()
    var tileNumber = 0;
    // To understand jsonData structure see GetJson() method
    for (var i = 0; i < jsonData.height; i++) {
        var tempArray = new Array();
        for (var j = 0; j < jsonData.width; j++) {
            tempArray.push(jsonData.layers[0].data[tileNumber]);
            tileNumber++;
        }
        tileMatrix.push(tempArray);
    }
    jsonData.layers[0].data = tileMatrix;
}

//==========================================================  
// Mockups json data for reading in the map in tiles and size 
//==========================================================
function GetJson() {
    var json = {
        "height": 10,
        "layers": [
            {
                "data":[8, 8, 7, 7, 7, 7, 7, 7, 8, 8, 8, 7, 11, 11, 19, 22, 14, 7, 7, 7, 8, 11, 11, 12, 12, 14, 14, 6, 6, 7, 8, 11, 11, 12, 18, 10, 10, 13, 6, 7, 7, 7, 11, 10, 18, 10, 10, 13, 5, 7, 7, 3, 9, 9, 9, 10, 15, 5, 7, 7, 7, 3, 4, 9, 9, 15, 9, 3, 3, 7, 7, 7, 3, 3, 4, 4, 3, 4, 7, 8, 8, 7, 7, 3, 3, 3, 7, 3, 7, 8, 8, 8, 7, 7, 7, 7, 8, 8, 8, 8],
         
                "height": 10,
                "name": "Tile Layer 1",
                "opacity": 1,
                "type": "tilelayer",
                "visible": true,
                "width": 10,
                "x": 0,
                "y": 0
        }],
        "orientation": "isometric",
        "properties": {

        },
        "tileheight": 32,
        "tilesets": [
            {
                "firstgid": 1,
                "image": "img\/64x32tiles.png",
                "imageheight": 128,
                "imagewidth": 128,
                "margin": 0,
                "name": "64x32tiles",
                "properties": {
                    "zaxis": "0"
                },
                "spacing": 0,
                "tileheight": 32,
                "tilewidth": 64
        },
            {
                "firstgid": 9,
                "image": "img\/64x48tiles.png",
                "imageheight": 256,
                "imagewidth": 128,
                "margin": 32,
                "name": "64x64tiles",
                "properties": {
                    "zaxis": "32"
                },
                "spacing": 0,
                "tileheight": 64,
                "tilewidth": 64
        },
            {
                "firstgid": 17,
                "image": "img\/64x64tiles.png",
                "imageheight": 256,
                "imagewidth": 128,
                "margin": 32,
                "name": "64x48tiles",
                "properties": {
                    "zaxis": "16"
                },
                "spacing": 0,
                "tileheight": 64,
                "tilewidth": 64
        }],
        "tilewidth": 64,
        "version": 1,
        "width": 10
    }

    return json;
}

//==========================================================  
// Online loading of Tiles with JSon
//==========================================================
//            var JsonUrl = "http://socialsurvivor.immoralstudios.com/miniMap2.json";            
//            $.ajax({
//                url: 'http://anyorigin.com/get?url=http://socialsurvivor.immoralstudios.com/miniMap2.json&callback=?',
//                type: 'GET',
//                dataType: "jsonp",
//                success: function (data) {
//                    tileHeight = data.contents.tileheight;
//                    tileWidth = data.contents.tilewidth / 2;
//                    tileXCount = data.contents.height;
//                    tileYCount = data.contents.width;
//                    map2 = data.contents.layers[0].data;                    
//                    // Gets all texture paths from Json file
////                    for(var key in data.contents.tilesets) {
////                        var tile = new Object();
////                        tile.path = data.contents.tilesets[key].image;
////                        tile.imageHeight = data.contents.tilesets[key].imageheight;
////                        tile.imageWidth = data.contents.tilesets[key].imagewidth;
////                        texturePathsArray[key++] = tile;    
////                    }
////                    tileArray = createTextureArray(texturePathsArray);
//                    tileArray = splitTileSet(data.contents.tilesets);
//                }
//            });